#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QMessageBox>
#include "dialogbase.h"
#include <QTextBrowser>
#include "readwidget.h"
#include "readenergywidget.h"
#include "remotecontrolwidget.h"
#include <QMdiArea>
#include <QMdiSubWindow>



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //初始化
    m_ComSet=nullptr;
    m_serialPort=new QSerialPort();
    m_remote_ctrl=nullptr;

}

MainWindow::~MainWindow()
{
    delete m_serialPort;
    delete m_ComSet;
    delete m_remote_ctrl;
    delete ui;
}

void MainWindow::appendLog(const QString &text)
{
    ui->textBrowser->append(text);
}



void MainWindow::on_action_ComSet_triggered()
{
    bool windowExists = false;
    for (QMdiSubWindow *subWindow : ui->mdiArea->subWindowList()) {
        if (qobject_cast<ComSetDialog*>(subWindow->widget())) {
            windowExists = true;
            subWindow->setFocus();
            break;
        }
    }

    if (!windowExists) {
        m_ComSet = new QMdiSubWindow(this);  // 父对象设为 mdiArea
        ComSetDialog* com = new ComSetDialog(this, m_ComSet);

        QWidget* widget=new QWidget(m_ComSet);
        QVBoxLayout* layout=new QVBoxLayout(widget);
        layout->addWidget(com);
        widget->setLayout(layout);
        // 使用正确的方法
        m_ComSet->setWidget(widget);  // 而不是 setWidget(com)
        m_ComSet->setWindowTitle("通讯参数配置");
        m_ComSet->setAttribute(Qt::WA_DeleteOnClose);

        ui->mdiArea->addSubWindow(m_ComSet);
        m_ComSet->move(50, 50);
        m_ComSet->setWindowFlags(m_ComSet->windowFlags() & ~Qt::WindowMaximizeButtonHint);
        //m_ComSet->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
        m_ComSet->setWindowIcon(QIcon(":/res/pix/title.png"));
        //m_ComSet->setStyleSheet("background-color: #D3D3D3; border: 1px solid #888;");
        m_ComSet->show();
    }

}

void MainWindow::popMessageBox(const QString &reason)
{
    // 这个回调是在主线程执行的，所以可以安全弹窗
    QMessageBox* box = new QMessageBox(this);  // 设置父对象
    box->setAttribute(Qt::WA_DeleteOnClose);   // 自动释放
    box->setWindowTitle("提示");
    box->setText(reason);
    box->setStandardButtons(QMessageBox::Ok);
    box->show();
}

void MainWindow::moveEvent(QMoveEvent *event)
{
    QMainWindow::moveEvent(event);

    // 计算主窗口移动的偏移量
    QPoint currentPos = this->pos();
    QPoint delta = currentPos - m_lastPos;
    m_lastPos = currentPos;

    // 移动所有子窗口
    moveAllChildWindows(delta);
}






void MainWindow::on_action_Energy_triggered()
{
//    ReadEnergyWidget* w=new ReadEnergyWidget(this,m_widget);
//    w->show();
    bool windowExists = false;
    for (QMdiSubWindow *subWindow : ui->mdiArea->subWindowList()) {
        if (qobject_cast<ReadEnergyWidget*>(subWindow->widget())) {
            windowExists = true;
            subWindow->setFocus();
            break;
        }
    }

    if (!windowExists) {
        m_readEnergy = new QMdiSubWindow(this);  // 父对象设为 mdiArea
        ReadEnergyWidget* com = new ReadEnergyWidget(this, m_readEnergy);

        QWidget* widget=new QWidget(m_readEnergy);
        QVBoxLayout* layout=new QVBoxLayout(widget);
        layout->addWidget(com);
        widget->setLayout(layout);
        // 使用正确的方法
        m_readEnergy->setWidget(widget);  // 而不是 setWidget(com)
        m_readEnergy->setWindowTitle("电能量数据");
        m_readEnergy->setAttribute(Qt::WA_DeleteOnClose);

        ui->mdiArea->addSubWindow(m_readEnergy);
        m_readEnergy->move(50, 50);
        m_readEnergy->setWindowFlags(m_readEnergy->windowFlags() & ~Qt::WindowMaximizeButtonHint);
        //m_ComSet->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
        m_readEnergy->setWindowIcon(QIcon(":/res/pix/title.png"));
        //m_ComSet->setStyleSheet("background-color: #D3D3D3; border: 1px solid #888;");
        m_readEnergy->show();
    }
}

void MainWindow::on_action_remotectrl_triggered()
{
    bool windowExists = false;
    for (QMdiSubWindow *subWindow : ui->mdiArea->subWindowList()) {
        if (qobject_cast<RemoteControlWidget*>(subWindow->widget())) {
            windowExists = true;
            subWindow->setFocus();
            break;
        }
    }

    if (!windowExists) {
        m_remote_ctrl = new QMdiSubWindow(this);  // 父对象设为 mdiArea
        RemoteControlWidget* com = new RemoteControlWidget(this, m_remote_ctrl);

        QWidget* widget=new QWidget(m_remote_ctrl);
        QVBoxLayout* layout=new QVBoxLayout(widget);
        layout->addWidget(com);
        widget->setLayout(layout);
        // 使用正确的方法A
        m_remote_ctrl->setWidget(widget);  // 而不是 setWidget(com)
        m_remote_ctrl->setWindowTitle("远程控制");
        m_remote_ctrl->setAttribute(Qt::WA_DeleteOnClose);

        ui->mdiArea->addSubWindow(m_remote_ctrl);
        m_remote_ctrl->move(50, 50);
        m_remote_ctrl->setWindowFlags(m_remote_ctrl->windowFlags() & ~Qt::WindowMaximizeButtonHint);
        //m_remote_ctrl->setWindowFlags(Qt::WindowMinimizeButtonHint | Qt::WindowCloseButtonHint);
        m_remote_ctrl->setWindowIcon(QIcon(":/res/pix/title.png"));
        m_remote_ctrl->show();
        //m_remote_ctrl->setStyleSheet("background-color: #D3D3D3; border: 1px solid #888;");
    }
}

void MainWindow::moveAllChildWindows(const QPoint &delta)
{
//    if (delta.isNull()) return;

//    // 获取所有子窗口
//    QList<QWidget*> childWindows = m_widget->findChildren<QWidget*>();

//    foreach (QWidget *child, childWindows)
//    {
//        // 确保是顶级窗口（没有父窗口或父窗口是contentWidget）
//        if (child->parentWidget() == m_widget || child->parent() == m_widget)
//        {
//            child->move(child->pos() + delta);
//        }
//    }
}
